#include "partition_driver.h"

#define STORAGE "storage"

void init_partition()
{
    
}

esp_err_t partition_flash_read_quick(size_t src_offset,void *dst, size_t size,const char *label)
{
    esp_err_t err;
    if ((dst == NULL)||(size > KEY_DATA_SECTOR_SIZE))
    {
        printf("ESP_ERR_INVALID_ARG\n");
        return ESP_ERR_INVALID_ARG;
    }
    
    const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, label);
    if(partition == NULL)
    {
        printf("Flash partition not found.\n");
        return ESP_FAIL;
    }

    err = esp_partition_read(partition,src_offset,dst,size);
    if(err != ESP_OK)
    {
        printf("Flash read failed.\n");
        return err;
    }

    return err;
}

esp_err_t partition_flash_write_quick(size_t src_offset,const void *src, size_t size,const char *label)
{
    esp_err_t err;
    if ((src == NULL)||(size > KEY_DATA_SECTOR_SIZE))
    {
        printf("ESP_ERR_INVALID_ARG\n");
        return ESP_ERR_INVALID_ARG;
    }

    const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, label);
    if(partition == NULL)
    {
        printf("Flash partition not found.\n");
        return ESP_FAIL;
    }

    err = esp_partition_erase_range(partition,src_offset,KEY_DATA_SECTOR_SIZE);
    if(err != ESP_OK)
    {
        printf("Flash erase failed.\n");
        return err;
    }

    err = esp_partition_write(partition,src_offset,src,size);
    if(err != ESP_OK)
    {
        printf("Flash write failed.\n");
        return err;
    }
    return err;
}



